import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: JSON.parse(localStorage.getItem('userInfo') || '{}'),
    userRole: localStorage.getItem('userRole') || ''
  }),
  
  getters: {
    isLoggedIn: (state) => !!state.token,
    isAdmin: (state) => state.userRole === 'SYSTEM_ADMIN',
    isTeacher: (state) => state.userRole === 'TEACHER',
    isGraduate: (state)=> state.userRole === 'GRADUATE'
  },
  
  actions: {
    setToken(token) {
      this.token = token
      localStorage.setItem('token', token)
    },
    
    setUserInfo(userInfo) {
      this.userInfo = userInfo
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },
    
    setUserRole(role) {
      this.userRole = role
      localStorage.setItem('userRole', role)
    },
    
    logout() {
      this.token = ''
      this.userInfo = {}
      this.userRole = ''
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      localStorage.removeItem('userRole')
    }
  }
}) 